package com.yjt.fresh.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;
import com.yjt.fresh.R;
import com.yjt.fresh.adpater.AdpRvFrgAfterSaleApply;
import com.yjt.fresh.api.ApiGetAfterSaleApply;
import com.yjt.fresh.base.BaseSimpleFragment;
import com.yjt.fresh.bean.BeanOrder;
import com.yjt.fresh.utils.PreferenceManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * Created by UPC on 2018/8/16.
 */

public class FrgAfterSaleApply extends BaseSimpleFragment {
    private static final int PAGE_SIZE = 20;
    @BindView(R.id.rvAppFrgAfterSaleApply)
    RecyclerView mRv;
    @BindView(R.id.srlAppFrgAfterSalApply)
    SwipeRefreshLayout mRefreshLayout;



    private AdpRvFrgAfterSaleApply mAdpAfterSaleApply;
    private int mNextRequestPage = 1;
    private boolean isLoadMore;

    @Override
    protected int getContentView() {
        return R.layout.app_frg_after_sale_apply;
    }

    @Override
    protected void initView(Bundle savedInstanceState, View view) {
        ButterKnife.bind(this, view);
    }

    @Override
    protected void initEvents() {
        initRv();
        initRefresh();
        getData();
    }

    private void initRefresh() {
        mAdpAfterSaleApply.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                isLoadMore = true;
                getData();
            }
        }, mRv);

        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mNextRequestPage = 1;
                isLoadMore = false;
                getData();
            }
        });
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (isVisibleToUser && mContext != null){
            mNextRequestPage = 1;
            isLoadMore = false;
            getData();
        }
    }

    private void getData() {
        ApiGetAfterSaleApply apiGetAfterSaleApply = new ApiGetAfterSaleApply(new HttpOnNextListener<List<BeanOrder>>() {

            @Override
            public void onNext(List<BeanOrder> beanOrders) {
                mAdpAfterSaleApply.setEmptyView(R.layout.view_empty);
                if (isLoadMore) {
                    setData(false, beanOrders);
                } else {
                    setData(true, beanOrders);
                    mRefreshLayout.setRefreshing(false);
                }
            }

            @Override
            public void onReLogin() {

            }
        }, mContext, true, false, false);

        Map<String, Object> map = new HashMap<>();
        map.put("currentPage", mNextRequestPage);
        map.put("SESSION_ID", PreferenceManager.getInstance().getUserInfo().getSESSION_ID());
        apiGetAfterSaleApply.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiGetAfterSaleApply);
    }

    private void setData(boolean isRefresh, List<BeanOrder> data) {
        mNextRequestPage++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdpAfterSaleApply.setNewData(data);
        } else {
            if (size > 0) {
                mAdpAfterSaleApply.addData(data);
                mAdpAfterSaleApply.loadMoreComplete();
            }else {
                mAdpAfterSaleApply.loadMoreEnd(true);
            }
        }

        if (size < 20){
            mAdpAfterSaleApply.loadMoreEnd(true);
        }
    }

    private void initRv() {
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        mRv.setLayoutManager(manager);
        List<BeanOrder> list = new ArrayList<>();
        mAdpAfterSaleApply = new AdpRvFrgAfterSaleApply(R.layout.app_recycle_item_frg_after_sale_apply_head, list);
        mRv.setAdapter(mAdpAfterSaleApply);
    }
}
